import Taro, {Component} from "@tarojs/taro";
import Index from "./pages/index";
import "@tarojs/async-await";
import "./app.less";
import "taro-ui/dist/style/index.scss";
import Api from "./api/api";
import {setGlobal, getGlobal} from "./api/globalData";

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释：
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }

class App extends Component {
  config = {
    pages: [
      "pages/index/index",
      "pages/note-detail/note-detail",
      "pages/my-collect/my-collect",
      "pages/create-collect/create-collect",
      "pages/me/me",
      "pages/discovery/discovery",
      "pages/read-record/read-record",
      "pages/collect-detail/collect-detail",
      "pages/search-result/search-result"
    ],
    window: {
      backgroundTextStyle: "dark",
      navigationBarBackgroundColor: "#fff",
      navigationBarTitleText: "",
      navigationBarTextStyle: "black",
      navigationStyle: 'custom'
    },
    tabBar: {
      color: "#cdcdcd",
      selectedColor: "#1296db",
      backgroundColor: "white",
      list: [
        {
          pagePath: "pages/index/index",
          iconPath: "assets/image/home.png",
          selectedIconPath: "assets/image/home_active.png",
          text: "首页"
        },
        {
          pagePath: "pages/me/me",
          iconPath: "assets/image/me.png",
          selectedIconPath: "assets/image/me_active.png",
          text: "我的"
        }
      ],
      position: "bottom"
    }
  };

  // req 就是userInfo
  async saveUser(req) {
    req.openId = getGlobal("openId");
    let {data} = await Api.login(req);

    if (data.success) {
      await Taro.setStorageSync("userInfo", data.data);
      setGlobal("userInfo", data.data);
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      Taro.eventCenter.trigger("userInfoEvent", data.data);
    } else {
      console.log(data.msg);
    }
  }

  async saveSystemInfo() {
    const sysInfo = await Taro.getSystemInfo();
    setGlobal("navBarHeight", sysInfo.statusBarHeight);
    setGlobal("screenHeight", sysInfo.screenHeight);
  }

  async componentDidMount() {

    this.saveSystemInfo()

    let openId = await Taro.getStorageSync("openId");
    let userInfo = await Taro.getStorageSync("userInfo");
    if (!openId) {
      let res = await Taro.login();
      if (res.code) {
        // 本地服务器获取session_key
        let sessionRs = await Api.codeSession(res.code);
        let session = sessionRs.data;
        if (session.success) {
          try {
            await Taro.setStorageSync("openId", session.data);
          } catch (e) {
            console.log(e);
          }
          setGlobal("openId", session.data);
        }
      } else {
        console.log("登录失败！" + res.errMsg);
      }
    } else {
      setGlobal("openId", openId);
    }

    if (!userInfo) {
      // 获取用户信息
      let setting = await Taro.getSetting();
      if (setting.authSetting["scope.userInfo"]) {
        // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
        let info = await Taro.getUserInfo();
        if (info) {
          // 可以将 res 发送给后台解码出 unionId
          let req = info.userInfo;
          await this.saveUser(req);
        }
      }
    } else {
      setGlobal("userInfo", userInfo);
    }
  }

  componentDidShow() {
  }

  componentDidHide() {
  }

  componentDidCatchError() {
  }

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render() {
    return <Index/>;
  }
}

Taro.render(<App/>, document.getElementById("app"));
